package com.lagou.edu.controller;

import com.lagou.edu.pojo.AuthCode;
import com.lagou.edu.pojo.User;
import com.lagou.edu.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Value("${lagou.message}")
    private String message;

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public ModelMap login(User user) {
        ModelMap model = new ModelMap();
        System.out.println(message);
        if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPwd())) {
            model.addAttribute("code", 400);
            model.addAttribute("message", "参数错误");
            return model;
        }
        User record = userService.getUnique(user);
        if (record != null) {
            model.addAttribute("code", 200);
            model.addAttribute("message", "登录成功");
            model.addAttribute("data", record);
            model.addAttribute("token", userService.getToken(record.getEmail()));
        } else {
            model.addAttribute("code", 404);
            model.addAttribute("message", "邮箱未注册或密码错误");
        }
        return model;
    }

    @RequestMapping("/register")
    public ModelMap register(HttpServletRequest request) {
        String email = request.getParameter("email");
        String pwd = request.getParameter("pwd");
        String code = request.getParameter("code");
        ModelMap model = new ModelMap();
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(pwd) || StringUtils.isEmpty(code)) {
            model.addAttribute("code", 400);
            model.addAttribute("message", "参数错误");
            return model;
        }
        ModelMap checkCode = userService.checkCode(new AuthCode(email, code));
        if (!"200".equals(checkCode.get("code").toString())) {
            model.addAttribute("code", 400);
            model.addAttribute("message", checkCode.get("message"));
            return model;
        }
        User record = userService.getUnique(new User(email));
        if (record == null) {
            User user = new User();
            user.setUsername(email);
            user.setEmail(email);
            user.setPwd(pwd);
            userService.save(user);
            model.addAttribute("code", 200);
            model.addAttribute("message", "注册成功");
        } else {
            model.addAttribute("code", 304);
            model.addAttribute("message", "邮箱已被注册");
        }
        return model;
    }

    @RequestMapping("/info/{token}")
    public ModelMap info(@PathVariable("token") String token) {
        ModelMap model = new ModelMap();
        if (StringUtils.isEmpty(token)) {
            model.addAttribute("code", 400);
            model.addAttribute("message", "参数错误");
            return model;
        }
        String email = userService.getEmail(token);
        if (StringUtils.isEmpty(email)) {
            model.addAttribute("code", 304);
            model.addAttribute("message", "未登录");
            return model;
        } else {
            model.addAttribute("code", 200);
            model.addAttribute("message", "已登录");
            model.addAttribute("data", email);
        }
        return model;
    }

}
